{extend name="public/container"}
{block name="content"}
<div id="myApp">
   <div class="douxue-filter padding-20 flex flex-between" style="margin-bottom:20px;">
        <el-form :inline="true" :model="where" size="mini" class="flex">
            <div class="flex flex-between">
              <el-form-item>
                <el-select
                    size="small"
                    v-model="where.status"
                    placeholder="是否显示"
                    :clearable=true>
                    <el-option label="显示" :value="1"></el-option>
                    <el-option label="隐藏" :value="2"></el-option>
                </el-select>
              </el-form-item>

              <el-form-item>
                <el-input size="small" v-model="where.cate_name" placeholder="请输入关键字">
                    <el-button slot="append" size="small" type="primary" @click="doLoadRows(1)" icon="el-icon-search"></el-button>
                </el-input>
              </el-form-item>
            </div>
        </el-form>
        <el-button type="primary" size="mini" @click="doModify()">新增文章</el-button>
    </div>

    <div class="padding-10">
        <el-table
            :data="rows"
            style="width: 100%"
            size="mini">
            <el-table-column
                prop="id"
                label="ID"
                width="80"
                align="left">
            </el-table-column>
            <el-table-column
                prop="cate_name"
                label="类别标题"
                width="200"
                align="left">
            </el-table-column>
            <el-table-column
                prop="title"
                label="文章标题"
                align="left">
            </el-table-column>
            <el-table-column
                prop="version"
                label="版本号"
                width="200"
                align="left">
            </el-table-column>
            <el-table-column
                prop="sort"
                label="排序"
                width="120"
                align="left">
            </el-table-column>
            <el-table-column
                prop="status"
                label="是否显示"
                width="120"
                align="left">
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.status"
                        active-color="#ff6a6c"
                        inactive-color="#999"
                        :active-value="1"
                        :inactive-value="2"
                        @change="doSetStatus(scope.row)">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column
                label=""
                width="90"
                align="left">
                <template slot-scope="scope">
                    <el-link type="primary" size="mini" :underline="false" @click="doModify(scope.row.id)">编辑</el-link>
                    <el-link type="danger" size="mini" :underline="false" @click="doDelete(scope.row.id)">删除</el-link>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination">
            <el-pagination
                small
                :hide-on-single-page="true"
                layout="prev, pager, next"
                :total="rowsCount"
                :page-size="where.limit"
                @current-change="doLoadRows">
            </el-pagination>
        </div>
    </div>
</div>
{/block}
{block name="script"}
<script>
    var organs = <?=isset($organs) ? json_encode($organs) : "[]"?>;
    var my = new Vue({
        el: "#myApp",
        data: {
            rowsCount: 0,
            rows: [],
            where:{
                page:1,
                limit: pageSize,
                status:'',
                cate_name:'',
            },
        },
        methods: {
            doLoadRows (pn) {
                this.where.page = pn || 1
                axios.get(Url({a:'get_list' , q:this.where})).then(res => {
                    this.rows = res.data.data
                    this.rowsCount = res.data.count
                })
            }
            // 设置状态
            ,doSetStatus (row) {
                axios.get(Url({a:'set_status',q:{status:row.status,id:row.id}})).then(res => {
                    this.$message({
                        message: res.data.msg,
                        type: res.data.code === 200 ? 'success' : 'error',
                        onClose: _ => {
                            if (res.data.code !== 0) {
                                this.doLoadRows()
                            }
                        }
                    })
                })
            }
            ,doModify ( id ) {
                if ( id != undefined && id > 0 ) {
                    location.href = Url({a:'add',q:{id:id}});
                } else {
                    location.href = Url({a:'add'});
                }
            }
            ,doDelete (id) {
                this.$confirm('确认删除该条记录?').then(_ => {
                    axios.get(Url({a:'delete' , q:{id:id}})).then(res => {
                        this.$message({
                            message: res.data.msg,
                            type: res.data.code === 200 ? 'success' : 'error',
                        })
                    }).catch(_ => {
                        this.$message({
                            message: '删除失败',
                            type: 'error'
                        })
                    })

                    return this.doLoadRows();
                }).catch(_ => {})
            }
        },
        mounted(){
            this.doLoadRows()
        }
    })
</script>
{/block}